﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;
using NhJump.Persistence.Context;
using NhJump.Persistence.Model;
using NhJump.Persistence.Querying.Executable;

namespace NhJump.Persistence.Querying.Definition
{
    public class CriteraQueryDefinition<T, TIdentifier> : IQueryDefinition<T, TIdentifier>
        where T : IDomainEntity<TIdentifier>
    {
        private readonly DetachedCriteria _detachedCriteria;

        public CriteraQueryDefinition(DetachedCriteria criteria)
        {
            _detachedCriteria = criteria;
        }

        public IExecutableQuery<T, TIdentifier> ToExecutableQuery(IReadableRepository<T, TIdentifier> readableRepository, IUnitOfWork unitOfWork)
        {
            return new CriteriaQuery<T, TIdentifier>(
                _detachedCriteria.GetExecutableCriteria(readableRepository.UnitOfWork.Session), unitOfWork);
        }
    }
}
